package com.wxl.common.util

import android.text.TextUtils
import com.wxl.common.AppContext
import io.reactivex.ObservableOnSubscribe
import java.io.*

/**
 * create file time : 2021/3/15
 * create user : wxl
 * subscribe :
 */

class FileCommon {
    companion object {

        fun getSdCardRootPath(): String {
            AppContext.appContext.getApplication()
                .getExternalFilesDir(android.os.Environment.DIRECTORY_PICTURES)?.let {
                    return it.absolutePath
                }
            return ""
        }

        fun getExternalFileRootPath(): String {
            val rootPath = getSdCardRootPath()
            if (!TextUtils.isEmpty(rootPath)) {
                val file = File("$rootPath/qbq")
                if (!file.exists()) {
                    file.mkdir()
                }
                return file.absolutePath
            }
            return ""
        }

        fun getExternalImageFile(): File? {
            val path = getExternalFileRootPath()
            if (!TextUtils.isEmpty(path)) {
                val dir = File("$path/pic")
                if (!dir.exists()) {
                    dir.mkdir()
                }
                val file = File("${dir.absolutePath}/${System.currentTimeMillis()}.png")
                file.createNewFile()
                return file
            }
            return null
        }

        /**
         * 读资源文件到内存文件里
         */
        fun readAssetsInFile(
            qbqAssetsPath: String,
            callback: ObservableUtil.Companion.OnNextCallback<File>
        ) {
            ObservableUtil.doNext(ObservableOnSubscribe<File> {
                val stream = javaClass.classLoader.getResourceAsStream("assets/${qbqAssetsPath}")
                val filed = File(getExternalFileRootPath() + "/up")
                if (!filed.exists()) {
                    filed.mkdirs()
                }

                val tf = File(filed.absolutePath + "/" + qbqAssetsPath)
                if (!tf.exists()) {
                    tf.createNewFile()
                }
                val fos = FileOutputStream(tf)
                var len = 0
                val buffer = ByteArray(1024)
                while (stream.read(buffer).let { len = it; it } != -1) {
                    fos.write(buffer, 0, len)
                    fos.flush()
                }
                stream.close()
                fos.close()
                it.onNext(tf)
            }, callback)
        }


        /**
         * 读取文件信息
         */
        fun readFileToText(file: File, callback: ObservableUtil.Companion.OnNextCallback<String>) {
            ObservableUtil.doNext(ObservableOnSubscribe<String> {
                val sf = StringBuffer()
                InputStreamReader(FileInputStream(file)).use { dataStream ->
                    val br = BufferedReader(dataStream)
                    var line = ""
                    while (br.readLine().also({ line = it }) != null) {
                        sf.append(line)
                    }
                    it.onNext(sf.toString())
                }
            }, callback)
        }

        /**
         * 获取采集器目录文件名称
         *
         * @param
         * @return
         */
        fun getCjqFile(): File {
            val file = File(getExternalFileRootPath() + "/cjq")
            if (!file.exists()) {
                file.mkdirs()
            }
            val cjqf = File(file.absolutePath + "/cjq.bin")
            if (!file.exists()) {
                file.createNewFile()
            }
            return cjqf
        }


        /**
         * 获取采集器目录文件夹名称
         *
         * @param
         * @return
         */
        fun getCjqFileDir(): File {
            val file = File(getExternalFileRootPath() + "/cjq")
            if (!file.exists()) {
                file.mkdirs()
            }
            return file
        }


        /**
         * 获取授权目录文件夹名称
         *
         * @param
         * @return
         */
        fun getLicenseFileDir(): File {
            val file = File(getExternalFileRootPath() + "/lis")
            if (!file.exists()) {
                file.mkdirs()
            }
            return file
        }


    }


}